<template>
  <div class="add">
    <DetailsFrom @addFn="addFn">
      <template>
        <div>
          <el-form ref="addForm" :model="addForm" label-position="top" :rules="addFormRules">
            <el-row>
              <el-row>
                <el-col :span="6">
                  <el-form-item label="库区" prop="areaId">
                    <el-cascader ref="cascader" v-model="addForm.areaId" :props="props" clearable @change="handleChange" />
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item label="库位编码" prop="code">
                    <el-input v-model="addForm.code" :disabled="true" clearable />
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item label="库位名称" prop="name">
                    <el-input v-model="addForm.name" placeholder="请输入" clearable />
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item label="温度类型" prop="temperatureType">
                    <el-select v-model="addForm.temperatureType" placeholder="请选择" clearable>
                      <el-option label="常温" value="CW" />
                      <el-option label="冷藏" value="LC" />
                      <el-option label="恒温" value="HW" />
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item label="承重类型" prop="bearingType">
                    <el-select v-model="addForm.bearingType" placeholder="请选择" clearable>
                      <el-option label="重型" value="ZX" />
                      <el-option label="中型" value="BX" />
                      <el-option label="轻型" value="QX" />
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item label="用途属性" prop="useType">
                    <el-select v-model="addForm.useType" placeholder="请选择" clearable>
                      <el-option label="入库缓存区" value="RKHCQ" />
                      <el-option label="出库缓存区" value="CKHCQ" />
                      <el-option label="存储区" value="CCQ" />
                      <el-option label="分拣区" value="FJQ" />
                      <el-option label="质检区" value="ZJQ" />
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="停用状态" prop="status">
                    <el-radio v-model="addForm.status" :label="0">启用</el-radio>
                    <el-radio v-model="addForm.status" :label="1">停用</el-radio>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-col :span="6">
                <el-form-item label="承载体积" prop="maxVolume">
                  <el-input v-model="addForm.maxVolume" placeholder="请输入内容" clearable>
                    <template slot="append">m²</template>
                  </el-input>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="承载上限" prop="maxNum">
                  <el-input v-model="addForm.maxNum" placeholder="请输入内容" clearable>
                    <template slot="append">个</template>
                  </el-input>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="承载重量" prop="maxWeight">
                  <el-input v-model="addForm.maxWeight" placeholder="请输入内容" clearable>
                    <template slot="append">Kg</template>
                  </el-input>
                </el-form-item>
              </el-col>

            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="库位排/列/层">
                  <el-row :gutter="30">
                    <el-col :span="8">
                      <el-input v-model="addForm.locationRow" clearable />
                    </el-col>
                    <el-col :span="8">
                      <el-input v-model="addForm.locationColumn" clearable />
                    </el-col>
                    <el-col :span="8">
                      <el-input v-model="addForm.locationLayer" clearable />
                    </el-col>
                  </el-row>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="库位长/宽/高">
                  <el-row :gutter="30">
                    <el-col :span="8">
                      <el-input v-model="addForm.locationLength" placeholder="请输入内容" clearable>
                        <template slot="append">cm</template>
                      </el-input>
                    </el-col>
                    <el-col :span="8">
                      <el-input v-model="addForm.locationWidth" placeholder="请输入内容" clearable>
                        <template slot="append">cm</template>
                      </el-input>
                    </el-col>
                    <el-col :span="8">
                      <el-input v-model="addForm.locationHigh" placeholder="请输入内容" clearable>
                        <template slot="append">cm</template>
                      </el-input>
                    </el-col>
                  </el-row>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </div>
      </template>
    </DetailsFrom>
  </div>
</template>

<script>
import { nextFn, addLocation, editLocation, searchLocation, warehouseList, areaList } from '@/api'
import DetailsFrom from '@/components/DetailsFrom'
import { messageFn } from '@/utils/myutils'
export default {
  // props: ['type'],
  components: { DetailsFrom },
  data() {
    return {
      addForm: {},
      id: null,
      addFormRules: {
        areaId: [{ required: true, message: '请选择库区', trigger: 'blur' }],
        name: [{ required: true, message: '请输入库位名称', trigger: 'blur' }],
        temperatureType: [{ required: true, message: '请选择温度类型', trigger: 'blur' }],
        bearingType: [{ required: true, message: '请选择承重类型', trigger: 'blur' }],
        useType: [{ required: true, message: '请选择用途属性', trigger: 'blur' }],
        status: [{ required: true, message: '请选择', trigger: 'blur' }],
        maxVolume: [{ required: true, message: '请输入小数', trigger: 'blur' }]
      },
      props: {
        emitPath: true,
        lazy: true,
        lazyLoad: this.getData
      }
    }
  },
  // 组件路由首位 修改meta的名字
  beforeRouteEnter(to, from, next) {
    if (to.params.id === 'null') {
      to.meta.title = '新建库位'
    } else {
      to.meta.title = '修改库位'
    }
    next()
  },
  async created() {
    if (this.$route.params.id === 'null') {
      const res = await nextFn('KW')
      this.$set(this.addForm, 'code', res.data.data)
      this.$set(this.addForm, 'status', 0)
      this.$forceUpdate()
    } else {
      const res = await searchLocation(this.$route.params.id)
      console.log(res)
      this.addForm = res.data.data
      console.log(this.addForm)
      this.id = this.addForm.areaId
      this.addForm.areaId = [this.addForm.warehouseId]
    }
  },
  methods: {
    // 选择器选择
    handleChange(value) {
      const nodesInfo = this.$refs['cascader'].getCheckedNodes()
      this.addForm.temperatureType = nodesInfo[0].value.temperatureType
      this.addForm.bearingType = nodesInfo[0].value.bearingType
      this.addForm.useType = nodesInfo[0].value.useType
      console.log(this.addForm.areaId)
    },
    // 编辑还是新增
    addFn() {
      this.$refs.addForm.validate(async(valid) => {
        if (!valid) return
        let res
        if (this.addForm.id) {
          res = await editLocation(this.addForm)
          console.log(1)
          this.addForm.warehouseId = this.addForm.areaId[0]
          this.addForm.areaId = this.addForm.areaId[1].id
        } else {
          res = await addLocation(this.addForm)
        }
        messageFn(res.data.isSuccess, this.$route.meta.title)
        this.$router.push({ name: 'location' })
      })
    },
    // 级联
    async getData(node, resolve) {
      const { level } = node
      if (!node.data) {
        await warehouseList({ status: 1 }).then(res => {
          const nodes = Array.from(res.data.data).map(item => {
            return {
              value: item.id,
              label: `${item.name}`,
              leaf: level >= 1
            }
          })
          resolve(nodes)
        })
      } else {
        await areaList({ warehouseId: node.value }).then(res => {
          const nodes = Array.from(res.data.data).map(item => {
            return {
              value: item,
              label: `${item.name}`,
              leaf: level >= 1
            }
          }
          )
          // 回显
          if (this.addForm.areaId) {
            console.log(this.addForm)
            nodes.forEach(item => {
              if (item.value.id === this.id) {
                console.log(1)
                this.addForm.areaId.push(item.value)
              }
            })
          }
          resolve(nodes)
        })
      }
    } }
}
</script>

<style lang="scss" scoped>
@import '@/styles/my-form.scss';

.el-col-8 {
  padding: 0;
}
</style>
